package com.wcpdoc.survey.survey.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.wcpdoc.survey.core.controller.BaseController;
import com.wcpdoc.survey.core.entity.PageIn;
import com.wcpdoc.survey.core.entity.PageOut;
import com.wcpdoc.survey.core.entity.PageResult;
import com.wcpdoc.survey.survey.entity.Survey;
import com.wcpdoc.survey.survey.entity.SurveyType;
import com.wcpdoc.survey.survey.entity.Paper;
import com.wcpdoc.survey.survey.service.SurveyService;
import com.wcpdoc.survey.sys.cache.DictCache;

/**
 * 调查控制层
 * 
 * v1.0 zhanghc 2017-06-11 09:13:23
 */
@Controller
@RequestMapping("/survey")
public class SurveyController extends BaseController{
	private static final Logger log = LoggerFactory.getLogger(SurveyController.class);
	
	@Resource
	private SurveyService surveyService;
	
	/**
	 * 到达调查列表页面 
	 * 
	 * v1.0 zhanghc 2017-06-11 09:13:23
	 * @return String
	 */
	@RequestMapping("/toList")
	public String toList() {
		try {
			return "/WEB-INF/jsp/survey/survey/surveyList.jsp";
		} catch (Exception e) {
			log.error("到达调查列表页面错误：", e);
			return "/WEB-INF/jsp/survey/survey/surveyList.jsp";
		}
	}
	
	/**
	 * 获取调查分类树型列表
	 * 
	 * v1.0 zhanghc 2017-06-11 09:13:23
	 * @return List<Map<String,Object>>
	 */
	@RequestMapping("/surveyTypeTreeList")
	@ResponseBody
	public List<Map<String, Object>> surveyTypeTreeList() {
		try {
			return surveyService.getSurveyTypeTreeList();
		} catch (Exception e) {
			log.error("获取调查分类树型列表错误：", e);
			return new ArrayList<Map<String,Object>>();
		}
	}
	
	/**
	 * 调查列表 
	 * 
	 * v1.0 zhanghc 2017-06-11 09:13:23
	 * @return pageOut
	 */
	@RequestMapping("/list")
	@ResponseBody
	public PageOut list(PageIn pageIn) {
		try {
			return surveyService.getListpage(pageIn);
		} catch (Exception e) {
			log.error("调查列表错误：", e);
			return new PageOut();
		}
	}
	
	/**
	 * 到达添加调查页面 
	 * 
	 * v1.0 zhanghc 2017-06-11 09:13:23
	 * @return String
	 */
	@RequestMapping("/toAdd")
	public String toAdd(Model model) {
		try {
			model.addAttribute("STATE", DictCache.getIndexDictlistMap().get("STATE"));
			return "/WEB-INF/jsp/survey/survey/surveyEdit.jsp";
		} catch (Exception e) {
			log.error("到达添加调查页面错误：", e);
			return "/WEB-INF/jsp/survey/survey/surveyEdit.jsp";
		}
	}
	
	/**
	 * 到达添加问卷页面
	 * 
	 * v1.0 zhanghc 2017年6月11日下午5:47:06
	 * @param id
	 * @return String
	 */
	@RequestMapping("/toPaperAddList")
	public String toPaperAddList(Integer id) {
		try {
			return "/WEB-INF/jsp/survey/survey/surveyPaperAddList.jsp";
		} catch (Exception e) {
			log.error("到达添加问卷页面错误：", e);
			return "/WEB-INF/jsp/survey/survey/surveyPaperAddList.jsp";
		}
	}
	
	/**
	 * 获取问卷分类树形列表
	 * 
	 * v1.0 zhanghc 2017年6月11日下午5:48:37
	 * @return List<Map<String,Object>>
	 */
	@RequestMapping("/paperAddListTypeTreeList")
	@ResponseBody
	public List<Map<String, Object>> paperAddListTypeTreeList() {
		try {
			return surveyService.getPaperAddListTypeTreeList();
		} catch (Exception e) {
			log.error("获取问卷分类树型列表错误：", e);
			return new ArrayList<Map<String,Object>>();
		}
	}
	
	/**
	 * 获取问卷列表 
	 * 
	 * v1.0 zhanghc 2017年6月11日下午5:50:36
	 * @param pageIn
	 * @return PageOut
	 */
	@RequestMapping("/paperAddList")
	@ResponseBody
	public PageOut paperAddList(PageIn pageIn) {
		try {
			return surveyService.getPaperAddListpage(pageIn);
		} catch (Exception e) {
			log.error("问卷列表错误：", e);
			return new PageOut();
		}
	}
	
	/**
	 * 完成添加调查
	 * 
	 * v1.0 zhanghc 2017-06-11 09:13:23
	 * @return pageOut
	 */
	@RequestMapping("/doAdd")
	@ResponseBody
	public PageResult doAdd(Survey survey) {
		try {
			survey.setUpdateTime(new Date());
			survey.setUpdateUserId(getCurrentUser().getId());
			survey.setState(2);
			surveyService.saveAndUpdate(survey);
			return new PageResult(true, "添加成功");
		} catch (Exception e) {
			log.error("完成添加调查错误：", e);
			return new PageResult(false, "添加失败：" + e.getMessage());
		}
	}
	
	/**
	 * 到达修改调查页面 
	 * 
	 * v1.0 zhanghc 2017-06-11 09:13:23
	 * @return String
	 */
	@RequestMapping("/toEdit")
	public String toEdit(Model model, Integer id) {
		try {
			Survey survey = surveyService.getEntity(id);
			model.addAttribute("survey", survey);
			Paper paper = surveyService.getPaper(survey.getPaperId());
			model.addAttribute("paper", paper);
			model.addAttribute("STATE", DictCache.getIndexDictlistMap().get("STATE"));
			
			SurveyType surveyType = surveyService.getSurveyType(id);
			model.addAttribute("surveyType", surveyType);
			return "/WEB-INF/jsp/survey/survey/surveyEdit.jsp";
		} catch (Exception e) {
			log.error("到达修改调查页面错误：", e);
			return "/WEB-INF/jsp/survey/survey/surveyEdit.jsp";
		}
	}
	
	/**
	 * 完成修改调查
	 * 
	 * v1.0 zhanghc 2017-06-11 09:13:23
	 * @return pageOut
	 */
	@RequestMapping("/doEdit")
	@ResponseBody
	public PageResult doEdit(Survey survey) {
		try {
			Survey entity = surveyService.getEntity(survey.getId());
			entity.setUpdateTime(new Date());
			entity.setUpdateUserId(getCurrentUser().getId());
			entity.setName(survey.getName());
			//entity.setState(survey.getState());
			entity.setStartTime(survey.getStartTime());
			entity.setEndTime(survey.getEndTime());
			entity.setDescription(survey.getDescription());
			entity.setPaperId(survey.getPaperId());
			surveyService.updateAndUpdate(entity);
			return new PageResult(true, "修改成功");
		} catch (Exception e) {
			log.error("完成修改调查错误：", e);
			return new PageResult(false, "修改失败：" + e.getMessage());
		}
	}
	
	/**
	 * 完成删除调查
	 * 
	 * v1.0 zhanghc 2017-06-11 09:13:23
	 * @return pageOut
	 */
	@RequestMapping("/doDel")
	@ResponseBody
	public PageResult doDel(Integer[] ids) {
		try {
			surveyService.deleteAndUpdate(ids);
			return new PageResult(true, "删除成功");
		} catch (Exception e) {
			log.error("完成删除调查错误：", e);
			return new PageResult(false, "删除失败：" + e.getMessage());
		}
	}
	
	/**
	 * 完成发布调查
	 * 
	 * v1.0 zhanghc 2017-06-11 09:13:23
	 * @return pageOut
	 */
	@RequestMapping("/doPublish")
	@ResponseBody
	public PageResult doPublish(Integer[] ids) {
		try {
			surveyService.doPublish(ids);
			return new PageResult(true, "发布成功");
		} catch (Exception e) {
			log.error("完成发布调查错误：", e);
			return new PageResult(false, "发布失败：" + e.getMessage());
		}
	}
	
	/**
	 * 到达分享调查链接页面 
	 * 
	 * v1.0 zhanghc 2017-06-11 09:13:23
	 * @return String
	 */
	@RequestMapping("/toSurveyUrl")
	public String toUrl() {
		try {
			return "/WEB-INF/jsp/survey/survey/surveyUrl.jsp";
		} catch (Exception e) {
			log.error("到达分享调查链接页面错误：", e);
			return "/WEB-INF/jsp/survey/survey/surveyUrl.jsp";
		}
	}
	
	/**
	 * 到达设置调查分类页面
	 * 
	 * v1.0 zhanghc 2017-05-25 16:34:59
	 * @return String
	 */
	@RequestMapping("/toSurveyTypeUpdate")
	public String toSurveyTypeUpdate() {
		try {
			return "/WEB-INF/jsp/survey/survey/surveySurveyTypeUpdate.jsp";
		} catch (Exception e) {
			log.error("到达设置调查分类页面错误：", e);
			return "/WEB-INF/jsp/survey/survey/surveySurveyTypeUpdate.jsp";
		}
	}
	
	/**
	 * 获取调查分类树型列表
	 * 
	 * v1.0 zhanghc 2017-05-25 16:34:59
	 * @return List<Map<String,Object>>
	 */
	@RequestMapping("/surveyTypeUpdateSurveyTypeTreeList")
	@ResponseBody
	public List<Map<String, Object>> surveyTypeUpdateSurveyTypeTreeList() {
		try {
			return surveyService.getSurveyTypeTreeList();
		} catch (Exception e) {
			log.error("获取调查分类树型列表错误：", e);
			return new ArrayList<Map<String,Object>>();
		}
	}
	
	/**
	 * 完成设置调查分类
	 * 
	 * v1.0 zhanghc 2017-05-25 16:34:59
	 * @param ids 用户ID
	 * @param surveyTypeId 调查分类ID
	 * @return PageResult
	 */
	@RequestMapping("/doSurveyTypeUpdate")
	@ResponseBody
	public PageResult doSurveyTypeUpdate(Integer[] ids, Integer surveyTypeId) {
		try {
			surveyService.doSurveyTypeUpdate(ids, surveyTypeId);
			return new PageResult(true, "设置成功");
		} catch (Exception e) {
			log.error("完成设置调查分类错误：", e);
			return new PageResult(false, "设置失败：" + e.getMessage());
		}
	}
}
